/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.libs.components;

import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TableFieldFactory;
import com.vaadin.ui.themes.Reindeer;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Spyros
 */
public class EditableTable extends Panel {

    private String[] visibleColumns;
    private String[] headers;
    private Table table;
    private BeanItemContainer beanItemContainer;
    private Class clazz;
    private TableFieldFactory tableFieldFactory;
    private String caption;
    private Button addButton;
    private Button removeButton;

    public EditableTable(String[] visibleColumns, String[] headers, BeanItemContainer beanItemContainer, Class clazz, TableFieldFactory tableFieldFactory, String caption) {
        this.visibleColumns = visibleColumns;
        this.headers = headers;
        this.beanItemContainer = beanItemContainer;
        this.clazz = clazz;
        this.tableFieldFactory = tableFieldFactory;
        this.caption = caption;

        initialize();
    }

    public Button getAddButton() {
        return addButton;
    }

    public Button getRemoveButton() {
        return removeButton;
    }

    public Table getTable() {
        return table;
    }

    public void refresh(BeanItemContainer beanItemContainer) {
        table.setContainerDataSource(beanItemContainer);
        if (visibleColumns != null) {
            table.setVisibleColumns(visibleColumns);
        }
        if (headers != null) {
            table.setColumnHeaders(headers);
        }
    }

    @Override
    public void setCaption(String caption) {
        table.setCaption(caption);
    }

    private void initialize() {
        this.setStyleName(Reindeer.PANEL_LIGHT);
        table = new Table();
        if (tableFieldFactory != null) {
            table.setTableFieldFactory(tableFieldFactory);
        }
        table.setEditable(true);
        table.setSelectable(true);
        table.setPageLength(5);
        table.setImmediate(true);
        if (caption != null) {
            table.setCaption(caption);
        }
        refresh(beanItemContainer);

        HorizontalLayout buttonsLayout = new HorizontalLayout();
        buttonsLayout.addComponent(getNewRecordButton());
        buttonsLayout.addComponent(getDeleteRecordButton(table));

        this.addComponent(table);
        this.addComponent(buttonsLayout);
    }

    private Button getNewRecordButton() {
        addButton = new Button("+", new Button.ClickListener() {

            @Override
            public void buttonClick(Button.ClickEvent event) {
                try {

                    table.addItem(clazz.getConstructor().newInstance());
                } catch (Exception ex) {
                    Logger.getLogger(EditableTable.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        return addButton;
    }

    private Button getDeleteRecordButton(final Table table) {
        removeButton = new Button("-", new Button.ClickListener() {

            @Override
            public void buttonClick(Button.ClickEvent event) {
                if (table.getValue() != null) {
                    table.removeItem(table.getValue());
                }
            }
        });
        return removeButton;
    }
}
